﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Input;

namespace MVVMLesson02
{
    public class Command : ICommand
    {
        public event EventHandler? CanExecuteChanged;

        public void RaiseCanExecuteChanged()
        {
            // 通知一下   要做状态检查了
            CanExecuteChanged?.Invoke(this, EventArgs.Empty);
        }

        public bool CanExecute(object? parameter)
        {
            return canExecute?.Invoke(parameter) != false;
        }

        public void Execute(object? parameter)
        {
            Console.WriteLine(parameter.ToString());
            action?.Invoke(parameter);
        }

        Action<object?> action;
        Func<object?, bool> canExecute;
        public Command(Action<object?> execute,Func<object?, bool> canExecute1 = null)
        {
            action = execute;
            canExecute = canExecute1;
        }
    }
}
